﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Temp.Controller;
using Temp.Model;
using Temp.NLG;
using Temp.Planner2;
using Temp.View;

namespace Temp.Planner2
{
    class PeerPlanner
    {
        private int timer;
        private DialogueView dialogueView;
        private StoryNodeController snc;
        private int currentActivityListValue;
        private StoryPlanner storyPlanner;
        private List<Item> listInventory;
        private Describer describer;
        private List<string> activityList;
        public PeerPlanner(StoryPlanner storyPlanner, DialogueView dialogueView, List<Item> listInventory, List<string> activityList) {
            this.storyPlanner = storyPlanner;
            this.dialogueView = dialogueView;
            this.activityList = activityList;
            currentActivityListValue = 0;
            timer = 0;
            snc = new StoryNodeController();
            describer = new Describer(storyPlanner);
            this.listInventory = listInventory;
        } 

        public void Update(){

            if (timer >= 6000)
            {
                timer = 0;
                peerGuideCritic();
            }
            else
            {
                timer++;
                
            }
        }

        public void peerGuideCritic() {
            List<Ontology> constructiveCritic = snc.getConstructiveCritic(storyPlanner.currentStoryNode.name);
            
            if(checkGoodBad() && constructiveCritic != null){
                List<Conversation> listC = new List<Conversation>();
                Conversation c = describer.fillUpPeerMessage(constructiveCritic[0].concept2, listInventory);
                listC.Add(c);
                dialogueView.listDialogue = listC;
                dialogueView.modifyListDialogue();
                dialogueView.char2Pic = null;
                dialogueView.isShowing = true;
                timer = 0;
            }
            
            else{

            List<Ontology> listDialogue = snc.getListDialogue(storyPlanner.currentStoryNode.name);
            Ontology temp;
            if (listDialogue != null) {
                for(int i = 0; i < listDialogue.Count; i++) {
                    temp = snc.getThreshold(listDialogue[i].concept2);

                    if (checkThreshold(temp))
                    {
                        List<Conversation> listC = new List<Conversation>();
                        Conversation c = describer.fillUpPeerMessage(listDialogue[i].concept2, listInventory);
                        listC.Add(c);
                        dialogueView.listDialogue = listC;
                        dialogueView.modifyListDialogue();
                        dialogueView.char2Pic = null;
                        dialogueView.isShowing = true;
                        timer = 0;
                    }

                }
            }
            }
        }


        private bool checkGoodBad(){
            int size = activityList.Count;
            if (size > 1) {

                int prevValue = snc.getCriteriaValue(activityList[size-2]);
                int currValue = snc.getCriteriaValue(activityList[size - 1]);

                if (prevValue < 0 && currValue > 0) {
                    return true;
                }
            }
            
            
                return false;
        }
        private bool checkThreshold(Ontology o) {
            int threshold = Convert.ToInt32(o.concept2);
            if (o.relation.ToLower().Contains("greaterthan")) {
                
                if (currentActivityListValue > threshold) {
                    return true;
                }
            }
            else if (o.relation.ToLower().Contains("lessthan")) {
                if (currentActivityListValue < threshold)
                {
                    return true;
                }
            }
            else if (o.relation.ToLower().Contains("greaterthanequal"))
            {
                if (currentActivityListValue >= threshold)
                {
                    return true;
                }
            }
            else if (o.relation.ToLower().Contains("lessthanequal"))
            {
                if (currentActivityListValue <= threshold)
                {
                    return true;
                }
            }

            return false;
        }

        public void calculateAcitvityListValue(string criteria){
            currentActivityListValue += snc.getCriteriaValue(criteria);
        }

        public void reduceActivityListValue(string criteria) {
            currentActivityListValue -= snc.getCriteriaValue(criteria);
            System.Diagnostics.Debug.WriteLine("MINUS VALUE: " + currentActivityListValue);
        }
    }
}